﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BhvTextureIndex : MonoBehaviour
{
    public Texture[] textures;
    [SerializeField]
    private int _index = 0;
    [SerializeField]
    public int index
    {
        get { 
            return _index;
        }
        set { 
            value = Mathf.Clamp(value,0,textures.Length-1);
            _index = value;
            this.ChangeTexture();
        }
    }

    #if UNITY_EDITOR
    private void OnValidate()
    {
        _index = Mathf.Clamp(_index,0,textures.Length-1);
        ChangeTexture();
        
    }
    #endif
    
    // Start is called before the first frame update
    
    public void ChangeTexture(){
        if(textures == null || textures.Length <=0)return;
        var render = this.GetComponent<Renderer>();
        if(!render)return;
        var texture = textures[_index];
        if(!texture)return;
        render.sharedMaterial.mainTexture = texture;
    }
}
